//
//  HWVoiceRoomChatFieldView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/16.
//

import UIKit
import IQKeyboardManagerSwift
class HWVoiceRoomChatFieldView: UIView {
    
    var fieldBg:UIView!
    var textField:UITextField!
    var sendButton:UIButton!
    var sendText:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        self.layoutIfNeeded()
        self.addCorner(conrners: [.topLeft,.topRight], radius: 18*widthScale)
        
        sendButton.layoutIfNeeded()
        let gradientColors = [UIColor.colorWithHexString(hexString: "F77105").cgColor,
                              UIColor.colorWithHexString(hexString: "F77105").cgColor,
                              
        ]
        
        //创建CAGradientLayer对象并设置参数
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        
        //设置渲染的起始结束位置（横向渐变）
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        
        //设置其CAGradientLayer对象的frame，并插入view的layer
        gradientLayer.frame = sendButton.bounds
        sendButton.layer.insertSublayer(gradientLayer, at: 0)
        sendButton.layer.cornerRadius = 17*widthScale
        sendButton.layer.masksToBounds = true

    }
    
}
extension HWVoiceRoomChatFieldView{
    private func initUI() -> Void {
        backgroundColor = UIColor.white

        fieldBg = UIView()
        fieldBg.backgroundColor = .colorWithHexString(hexString: "#F6F7F9")
        fieldBg.layer.cornerRadius = 20*widthScale
        addSubview(fieldBg)
        
        textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 14*widthScale)
        textField.textColor = .colorWithHexString(hexString: "#333333")
        textField.placeholder = "请输入内容..."
        textField.tintColor = .white
        fieldBg.addSubview(textField)
        
        sendButton = .init(type: .custom)
        sendButton.setTitle("发送", for: UIControl.State.normal)
        sendButton.setTitleColor(.white, for: UIControl.State.normal)
        sendButton.titleLabel?.font = .systemFont(ofSize: 16*widthScale)
        sendButton.backgroundColor = UIColor.blue
        sendButton.layer.cornerRadius = 17*widthScale
        sendButton.addTarget(self, action: #selector(sendButtonClick), for: UIControl.Event.touchUpInside)
        addSubview(sendButton)
        
        
        
        
    }
    
    private func initFrame() -> Void {
        fieldBg.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(sendButton.snp.left).offset(-10*widthScale)
            make.top.equalTo(10*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-10*widthScale)
        }
        
        sendButton.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(34*widthScale)
            make.width.equalTo(63*widthScale)
            make.centerY.equalTo(fieldBg)
        }
        
        sendButton.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
        sendButton.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
    }
}

extension HWVoiceRoomChatFieldView{
    @objc func sendButtonClick() -> Void {
        sendText?(textField.text ?? "")
    }
}
